var storageId;
$(function () {
    storageId = $.getUrlParam("id");
    
    //是否验证成功
    $('#editStorageForm').validationEngine('attach', {
        validationEventTrigger : 'blur',
        promptPosition: 'inline',
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
            if (valid){
                if (storageId == 'null') {
                	addstorageInfo();
                } else {
                	modifystorageInfo();
                }
            }
        }
    });
    
    //如果storage为null，则是增加操作，否则是编辑操作.
    //这个地方使用的return，后面的语句将执行不到
    if(storageId == 'null'){
    	return;
    }else{
    	echostorageInfo();
    }
});

//点击编辑回显数据
function echostorageInfo(){
    $.ajax({
        url: "../storage/echoStorageInfo.htm",
        cache: false,
        async : false,
        type: "post",
        dataType: "json",
	    data: {storageId: storageId},
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                $("#editStorageForm").formatForm(data);
                return;
                
                
            }
        }
    });
}

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//修改仓库信息
function modifystorageInfo(){
    $.ajax({
        url: "../storage/modifyStorageInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data : $("#editStorageForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../storage/getStorageInfoByPage.htm'});
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
            closeForm();
        }
    });
}

//增加仓库信息
function addstorageInfo() {
    $.ajax({
        url: "../storage/addToStoreInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#editStorageForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../storage/getStorageInfoByPage.htm'});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
                closeForm();
            }
        }
    });
}